/*
 * Externalizer.java
 *
 * Created on 29 de septiembre de 2006, 04:24 PM
 *

 */

package inc.jmc.cyberboard.persistence;
import inc.jmc.cyberboard.ui.CyberManager;
import inc.jmc.cyberboard.ui.PCTimers;
import inc.jmc.cyberboard.ui.ventas.CatalogoGridProductos;
import inc.jmc.cyberboard.ui.ventas.DetalleGridVentas;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

import org.apache.log4j.Logger;
/**
 *
 * @author Juan Miguel Olguin Salguero
 */
public class Externalizer {
    static Logger logger = Logger.getLogger(Externalizer.class.getName());
    /** Creates a new instance of Externalizer */
    private static Externalizer external=null;
    private Externalizer() {
    }
    public static Externalizer getInstance()
    {
        if(external==null)
        {
            external=new Externalizer();
        }
        return external;
    }
    public void savePCTimers(CyberManager cyberM) throws java.io.IOException
    {
        
        JInternalFrame[] jFrames=cyberM.getAllFrames();
        java.util.ArrayList<PCTimers> arry=new java.util.ArrayList<PCTimers>();
        int nwins=jFrames.length;
        for(int i=0;i<nwins;++i)
        {
            if(jFrames[i].getClass().getName().indexOf(PCTimers.class.getName())>=0)
            {
                
                PCTimers aux=(PCTimers )jFrames[i];
                arry.add(aux);
            }
        }
        inc.jmc.cyberboard.persistence.PCTimersFrmIO timersio=new inc.jmc.cyberboard.persistence.PCTimersFrmIO();
        try{
        timersio.save(arry);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        
        }
    }
    public void loadPCTimers(CyberManager cyberM,inc.jmc.cyberboard.InternalClock clock) throws Exception
    {
        
        PCTimersFrmIO pctimers=new PCTimersFrmIO();
        java.util.ArrayList<PCTimers> arry=new java.util.ArrayList<PCTimers>();
        arry=pctimers.load(clock);
        JDesktopPane jdesktop=cyberM.getJDesktopPane2();
        JInternalFrame[] ji=jdesktop.getAllFrames();
        for(int i=0;i<ji.length;++i)
        {
            if(ji[i].getClass().getName().indexOf("PCTimer")>=0)
            {
                jdesktop.remove(ji[i]);
                logger.info("Eliminando frame "+ji[i]);
            }
        }
        ///ya que tenemos el mismo pctimer 
        for(int i=0;i<arry.size();++i)
        {
        	if( !  cyberM.isALLOWED_To_ADD_A_PCTimers())
        		break;
            PCTimers aux=(PCTimers )arry.get(i);
            jdesktop.add(aux);
            //
            System.out.println("agregando timer al desktop ");
            
            CatalogoGridProductos catalogoGridProd = cyberM.getVentasFrm().getCatalogoGridProd();
            DetalleGridVentas detalleGrid = cyberM.getVentasFrm().getDetalleGrid();
            
            aux.setCatalogoGridProd(catalogoGridProd);
            aux.setDetalleGrid(detalleGrid);
            aux.setCyberManager(cyberM);
            if(cyberM.getAuthorizedUserTipo().equalsIgnoreCase("Administrador")){
            		
            		//
            	aux.setClosable(cyberM.getDeletePcTimersMenuItem().getState());
            }
            else{
            	aux.setClosable(false);
            }
            
            aux.getNombreCompuTxt().setEditable(cyberM.getAuthorizedUserTipo().equalsIgnoreCase("Administrador"));
            aux.getIpTxt().setEditable(cyberM.getAuthorizedUserTipo().equalsIgnoreCase("Administrador"));
            
            aux.setRelojInternoTicks(clock);
            aux.setEvents();
            System.out.println("Reloj interno tick agregado");
                    
        }
        jdesktop.updateUI();
        
        /*
        FileInputStream fis = new FileInputStream("D:\\jmdevelopment\\CyberCaffeeNet\\pctimers.tmp");
        ObjectInputStream ois = new ObjectInputStream(fis);
        java.util.ArrayList arry=(java.util.ArrayList)ois.readObject();
        ois.close();
                
        JDesktopPane jdesktop=cyberM.getJDesktopPane1();
        for(int i=0;i<arry.size();++i)
        {
            PCTimers aux=(PCTimers )arry.get(i);
            jdesktop.add(aux);
            //
            System.out.println("agregando timer al desktop ");
            aux.setRelojInternoTicks(clock);
            aux.setEvents();
            System.out.println("Reloj interno tick agregado");
                    
        }
        jdesktop.updateUI();*/
    }
    
    public void saveConfiguracionTarfiaFrm(CyberManager cyberM)throws Exception 
    {
        ConfiguracionTarifaFrmIO configIO= new ConfiguracionTarifaFrmIO();
        configIO.save(cyberM.getTarifasFrm(),cyberM);
    }
    public void loadConfiguracionTarfiaFrm(CyberManager cyberM)throws Exception
    {
        ConfiguracionTarifaFrmIO configIO= new ConfiguracionTarifaFrmIO();
        configIO.load(cyberM);
    }
    
    
}
